﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Transactions;

public partial class VietBai : System.Web.UI.Page
{
    chodientuDataContext db = new chodientuDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_dangbai_Click(object sender, EventArgs e)
    {
        Baiviet bv = new Baiviet();
        bv.Tieude = tieudeid.Text;
        bv.Noidung = noidungid.Text;
        bv.Ngaydang = DateTime.Now;
        bv.Quyenriengtu = drp_quyenriengtu.SelectedValue.ToString();
        bv.Mablog = Session["user"].ToString()+" Blog";

        //them bai viet
        using (TransactionScope ts = new TransactionScope())
        {
            try
            {
                int n = db.Nhatkies.Select(q => q).Count();
                if (n == 0)
                {
                    Nhatky nk = new Nhatky();
                    nk.Mablog = Session["user"].ToString() + " Blog";
                    nk.Tenblog = "My Blog";
                    nk.Username = Session["user"].ToString();

                    //them blog
                    db.Nhatkies.InsertOnSubmit(nk);
                    db.SubmitChanges();
                }

                db.Baiviets.InsertOnSubmit(bv);
                db.SubmitChanges();

                ts.Complete();
                Response.Redirect("Myblog.aspx");
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
    protected void btn_huybo_Click(object sender, EventArgs e)
    {
        Response.Redirect("user.aspx");
    }
}